package com.xp.flower.qsd.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * @description 配置
 * @projectName mysteel-advert-webapp
 * @author  运营中心 石艳
 * @createTime 2017年2月23日 下午4:52:31
 * @version 1.0
 */
public class ConfigUtils
{
	/** 图片保存路劲 */
	public static String PIC_SAVE_PATH;
	/** 图片展示路劲 */
	public static String PIC_SHOW_PATH;
	
	/** 分页大小 */
	public static Integer PAGE_SIZE;
	
	/** 可试用次数 */
	public static Integer TEST_APPLY_NUM;
	
	/** 常用搜索条件最大数 */
	public static Integer MAX_QUERY_RECORD_SIZE;
	
	/** 发票号最大数 */
	public static Integer MAX_INVOICECODE_NUM;
	
	/** 可延期天数 */
	public static Integer MAX_DELAY_DAYS;
	
	/** 试用天数 */
	public static Integer TEST_DAYS;
	
	/** 正式即将过期时间（天） */
	public static Integer FORMAL_LIMIT_DAYS;
	/** 测试即将过期时间（天） */
	public static Integer TEST_LIMIT_DAYS;
	
	/** 搜搜刚品种http接口 */
	public static String SS_BREED_URL;
	
	static{
		load();
	}
	
	/** 加载参数 */
	public static void load(){
		
    	InputStream inputStream = null;
    	try {
    		inputStream 		= 	Thread.currentThread().getContextClassLoader().getResourceAsStream("config/config.properties");		
    		Properties props	=	new Properties();
    		props.load(new InputStreamReader(inputStream, "UTF-8"));
    		
    		// 设置参数
    		setPros(props);
    		
    	} catch (IOException e) {
    		throw new RuntimeException("Error loading properties file.", e);
    	} finally {
    		if (inputStream != null) {
    			try {
    				inputStream.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
	}

	/** 设置参数 */
	private static void setPros(Properties props) {
		
		if(props == null) return ;
		
		PIC_SAVE_PATH			=	getProp(props, "PIC_SAVE_PATH", "");
		if(PIC_SAVE_PATH.length() > 0)
		{
			File file = new File(PIC_SAVE_PATH);
			if(!file.exists()) file.mkdir();
		}
		PIC_SHOW_PATH			=	getProp(props, "PIC_SHOW_PATH", "upload");
		
		PAGE_SIZE				=	getPropInt(props, "PAGE_SIZE", 10);
		TEST_APPLY_NUM			=	getPropInt(props, "TEST_APPLY_NUM", 0);
		MAX_QUERY_RECORD_SIZE	=	getPropInt(props, "TEST_APPLY_NUM", 10);
		MAX_INVOICECODE_NUM		=	getPropInt(props, "MAX_INVOICECODE_NUM", 1);
		MAX_DELAY_DAYS			=	getPropInt(props, "MAX_DELAY_DAYS", 0);
		TEST_DAYS				=	getPropInt(props, "TEST_DAYS", 0);
		FORMAL_LIMIT_DAYS		=	getPropInt(props, "FORMAL_LIMIT_DAYS", 0);
		TEST_LIMIT_DAYS			=	getPropInt(props, "TEST_LIMIT_DAYS", 0);
		
		SS_BREED_URL			=	getProp(props, "SS_BREED_URL", "");
	}
	
	/** 从Properties中读取属性值 并转换成Integer */
	private static Integer getPropInt(Properties props, String key, Integer defaultValue)
	{
		String prop = getProp(props, key, null);
		
		return prop == null ? defaultValue : Integer.parseInt(prop);
	}
	
	/** 从Properties中读取属性值 */
	private static String getProp(Properties props, String key, String defaultValue)
	{
		String prop = props.getProperty(key, defaultValue);
		
		return prop == null ? null : prop.trim();
	}
}
